package com.originskyseed.platform.cms.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.CmsType;
import com.originskyseed.platform.cms.service.ICmsTypeService;
import com.originskyseed.platform.cms.vo.request.CmsTypeRequestVO;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 新闻类型表Controller类（OSI_CMS_TYPE）
 *
 * @author wlj
 */
@Controller
@RequestMapping("/cmsType")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class CmsTypeController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmsTypeController.class);

	@DubboReference
	private ICmsTypeService cmsTypeService;

	/**
	 * 新闻或通知公告分类列表_分页查询 OW 运管
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsTypeList.json")
	@RequiredParams(names = { "pageNo", "pageSize", "type" })
	public BaseResponseBody<BasePage<CmsType>> cmsTypeList(HttpServletRequest request) {
		BaseResponseBody<BasePage<CmsType>> result = null;
		try {
			// 请求对象
			CmsTypeRequestVO requestVO = this.getParameterBean(CmsTypeRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			if (userInfo.getSourceSys() == 2) {
				requestVO.setBelongPlatform(2);
			} else if (userInfo.getSourceSys() == 1) {
				requestVO.setBelongPlatform(1);
			}
			requestVO.setIsDel(2);
			BasePage<CmsType> page = new BasePage<CmsType>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.cmsTypeService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<CmsType>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<CmsType>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询新闻类型树
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsTypeTree.json")
	public BaseResponseBody cmsTypeTree(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			CmsTypeRequestVO requestVO = this.getParameterBean(CmsTypeRequestVO.class);
			// 请求对象
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			/*
			 * if (userInfo.getSourceSys() == 2) { requestVO.setBelongPlatform(2); } else if (userInfo.getSourceSys() == 1) { requestVO.setBelongPlatform(1); }
			 */
			requestVO.setIsDel(2);
			List<CmsType> cmsTypeTree = this.cmsTypeService.cmsTypeTree(requestVO.toMap());
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), cmsTypeTree);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息_新建分类信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsTypeSave.json")
	@RequiredParams(names = {})
	public BaseResponseBody<CmsType> cmsTypeSave(HttpServletRequest request) {
		BaseResponseBody<CmsType> result = null;
		try {
			// 请求对象
			CmsType cmsType = this.getParameterBean(CmsType.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			if (StringUtils.isNotBlank(cmsType.getId())) {
				this.cmsTypeService.updateByPrimaryKey(cmsType);
			} else {
				//校验名称是否重复
				CmsType isNull = this.cmsTypeService.selectIsNullByTypeName(cmsType.getName());
				if(isNull == null) {
					cmsType.setCreator(userInfo.getName());
					cmsType.setCreatorId(userInfo.getId());
					cmsType.setIsDel(2);
					this.cmsTypeService.insertSelective(cmsType);
				}else {
					return new BaseResponseBody<CmsType>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.TYPE_NAME_IS_NOTNULL.getMsg()), null);
				}
			}
			result = new BaseResponseBody<CmsType>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_TYPE_SAVE_SUCCESS.getMsg()), cmsType);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<CmsType>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_TYPE_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsTypeDelete.json")
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> cmsTypeDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			this.cmsTypeService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_TYPE_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_TYPE_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 新闻类型选择列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectCmsTypeList.json")
	@RequiredParams(names = {})
	public BaseResponseBody selectCmsTypeList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			CmsType cmsType = this.getParameterBean(CmsType.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			List<CmsType> resultList = null;
			cmsType.setIsDel(2);
			if (userInfo.getSourceSys() == 2 && userInfo.getType() == 6) { // 业务员
				cmsType.setBelongPlatform(3);
				resultList = this.cmsTypeService.selectList(cmsType);
			}
			if (userInfo.getSourceSys() == 1 && userInfo.getType() == 11) { // 主播
				cmsType.setBelongPlatform(2);
				resultList = this.cmsTypeService.selectList(cmsType);
			}
			if (userInfo.getSourceSys() == 0 && (userInfo.getType() == 2 || userInfo.getType() == 5)) { // ow 运管
				cmsType.setBelongPlatform(1);
				resultList = this.cmsTypeService.selectList(cmsType);
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), resultList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 新闻类型详情
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/selectByCmsTypeId.json")
	@RequiredParams(names = "id") // 类型ID
	public BaseResponseBody selectByCmsTypeId(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			String id = this.getParameterString("id", null);// 新闻ID
			CmsType cmsType = this.cmsTypeService.selectByPrimaryKey(id);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), cmsType);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 根据新闻ID，优先级自定义升序、降序
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateUpOrDown.json")
	@RequiredParams(names = { "id", "upOrDown" })
	public BaseResponseBody updateUpAndDown(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);
			Integer upOrDown = this.getParameterInteger("upOrDown", null);
			// 获取当前的优先级
			CmsType selectByPrimaryKey = this.cmsTypeService.selectByPrimaryKey(id);
			Integer currentPrority = selectByPrimaryKey.getPrority();
			Integer updatePrority = currentPrority;

			// 等于0是升序 1是降序
			if (upOrDown == 0) {
				currentPrority += 1;
			} else {
				currentPrority -= 1;
			}

			// 将另外一个升序或降序
			CmsType cmsType = this.cmsTypeService.selectCmsTypeByPrority(currentPrority);
			if (cmsType != null) {
				cmsType.setPrority(updatePrority);
			}
			CmsType updateCms = new CmsType();
			updateCms.setId(id);
			updateCms.setPrority(currentPrority);
			if (cmsType != null) {
				this.cmsTypeService.updatePrority(cmsType, updateCms);
			} else {
				this.cmsTypeService.updateByPrimaryKeySelective(updateCms);
			}

			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.UPDATE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.UPDATE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 是否置顶
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateIsTop.json")
	@RequiredParams(names = { "id" }) // 新闻ID
	public BaseResponseBody updateIsTop(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			String id = this.getParameterString("id", null);
			CmsType cmsType = new CmsType();
			cmsType.setId(id);
			cmsType.setIsTop(1);
			cmsType.setUpdateTime(new Date());
			this.cmsTypeService.updateIsTop(cmsType);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.UPDATE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.UPDATE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
